The Art of Clean Code by Christian Mayer

The Art of Clean Code by Christian Mayer

Author:Christian Mayer [Mayer, Christian]
Language: eng
Format: epub, pdf
ISBN: 9781718502192
Published: 2022-05-10T00:00:00+00:00


In other words, you should minimize the dependencies of your code objects. By reducing dependencies between code objects, you reduce the complexity of your code and, in turn, improve maintainability. One specific implication is that every object should call only its own methods or methods from adjacent objects rather than call methods of objects it obtains from calling a method of an adjacent object. For the sake of explanation, let’s define two objects A and B as friends if A calls a method provided by B. Simple. But what if B’s method returns a reference to object C? Now, object A may perform something like this: B.method_of_B().method_of_C(). This is called chaining of method calls—in our metaphor, you talk to a friend of your friend. The Law of Demeter says to talk only to your immediate friends, so it discourages this type of method chaining. This may sound confusing at first, so let’s dive into the practical example shown in Figure 4-3.

Figure 4-3: Law of Demeter: talking only to your friends to minimize dependencies



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.